<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="导航名称" prop="navigationName">
      <el-input v-model="dataForm.navigationName" placeholder="导航名称"></el-input>
    </el-form-item>
    <el-form-item label="父id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父id"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder="排序"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          navigationId: 0,
          navigationName: '',
          parentId: '',
          sort: ''
        },
        dataRule: {
          navigationName: [
            { required: true, message: '导航名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父id不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.navigationId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.navigationId) {
            this.$http({
              url: this.$http.adornUrl(`/blog/navigation/info/${this.dataForm.navigationId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.navigationName = data.navigation.navigationName
                this.dataForm.parentId = data.navigation.parentId
                this.dataForm.sort = data.navigation.sort
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/blog/navigation/${!this.dataForm.navigationId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'navigationId': this.dataForm.navigationId || undefined,
                'navigationName': this.dataForm.navigationName,
                'parentId': this.dataForm.parentId,
                'sort': this.dataForm.sort
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
